<script setup>
import { inject, onMounted, ref } from 'vue'
import { artGetChannelsService } from '../../../api/article'

const $echarts = inject('echarts')
const categorys = ref([])
const data = ref([])

const categoryList = async () => {
  const result = await artGetChannelsService()
  categorys.value = result.data.data
}

const setData = () => {
  data.value = categorys.value.map((v) => ({
    value: 1,
    name: v.categoryName
  }))
  console.log('饼图', data.value)
}
// Transform categorys.value into data array
onMounted(async () => {
  await categoryList()
  setData()

  const myChart = $echarts.init(document.getElementById('twoChart'))
  myChart.setOption({
    tooltip: {
      trigger: 'item'
    },
    legend: {
      top: '5%',
      left: 'center'
    },
    series: [
      {
        name: 'Access From',
        type: 'pie',
        radius: ['40%', '70%'],
        avoidLabelOverlap: false,
        padAngle: 5,
        itemStyle: {
          borderRadius: 10
        },
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 40,
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data: data.value
      }
    ]
  })
})
</script>

<template>
  <h2>类型分布图</h2>
  <div class="chart" id="twoChart">图标的容器</div>
</template>

<style scope>
.chart {
  width: 350px;
  height: 240px;
}
</style>
